package com.imm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imm.common.Result;
import com.imm.pojo.Employee;
import com.imm.service.IEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    private IEmployeeService employeeService;

    /**
     * 员工登录
     *
     * @param employee 员工信息类
     * @param request  请求
     * @return 响应
     */
    @PostMapping("/login")
    public Result<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {
        return employeeService.login(employee, request);
    }

    /**
     * 新增员工
     * @param employee 员工信息
     * @return 相应
     */
    @PostMapping
    public Result<String> save(@RequestBody Employee employee, HttpServletRequest request){
        log.info("员工信息：{}", employee.toString());
        return employeeService.saveEmployee(employee, request);
    }

    /**
     * 分页查询
     * @param page 页数
     * @param pageSize 一页多少条
     * @param name 员工姓名
     * @return
     */
    @GetMapping("/page")
    public Result<Page> page(int page, int pageSize, String name) {
        log.info("请求信息：page = {}, pageSize = {}", page, pageSize);
        return employeeService.pageEmployee(page, pageSize, name);
    }

    @PutMapping
    public Result<String> update(@RequestBody Employee employee, HttpServletRequest request) {
        return employeeService.updateEmployee(employee, request);
    }

    @GetMapping("{id}")
    public Result<Employee> getOne(@PathVariable(value = "id") Long id) {
        return employeeService.getEmployee(id);
    }

    /**
     * 退出登录
     * @param request 请求
     * @return 相应
     */
    @PostMapping("/logout")
    public Result<String> logout(HttpServletRequest request){
        return employeeService.logout(request);
    }
}
